package view;

import javax.swing.*;
import javax.swing.border.Border;

import net.miginfocom.swing.MigLayout;

import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ItemListener;
import java.awt.geom.*;
import java.util.*;

/**
 * Floor panel for manager's view.
 * 
 * @author Kenny Chong, Dan End, James Ju
 * CSE 333 : User Interface
 * Project HMSystem
 */
public class FloorPanel extends JPanel
{
	private ArrayList<JPanel> roomList;
	private ArrayList<JPanel> elevatorList;
	
	private JLabel roomLabel;
	private JLabel elevatorLabel;
	
	private MigLayout layout;
	
	private JToolBar buttonBar;
	
	private JLabel guestLabel;
	private JTextField nameField;
	private JButton searchButton;
	
	private JLabel highLightRoomLabel;
	private JComboBox highLightBox;
	
	private JButton cameraButton;
	
	private JPanel drawPanel;
	
	/**
	 * Construct floor panel.
	 * @param numOfRooms
	 */
	public FloorPanel()
	{
		// Initialize roomList.
		roomList = new ArrayList<JPanel>();
		
		// Initialize elevatorList;
		elevatorList = new ArrayList<JPanel>();
	
		buttonBar = new JToolBar();
	
		guestLabel = new JLabel("Search Guest By Name:");
		nameField = new JTextField(10);
		searchButton = new JButton("Find");
		searchButton.setToolTipText("Search For Guest By Name");
		
		highLightRoomLabel = new JLabel("Highlight By:");
		highLightBox = new JComboBox();
		highLightBox.setToolTipText("Highlight Certain Rooms According To Selected Specs");
		
		highLightBox.addItem("None");
		highLightBox.addItem("Occupied");
		highLightBox.addItem("Cleaned");
		
		cameraButton = new JButton(new ImageIcon("images/camera.png"));
		cameraButton.setToolTipText("View Current Floor's Camera(s)");
		
		drawPanel = new JPanel();
			
		buttonBar.add(cameraButton);
		
		buttonBar.addSeparator();
		
		buttonBar.add(highLightRoomLabel);
		buttonBar.add(highLightBox);
		
		buttonBar.addSeparator();
		
		buttonBar.add(guestLabel);
		buttonBar.add(nameField);
		buttonBar.add(searchButton);
		
		buttonBar.setLayout(new FlowLayout());
		
		// Set layout.
		MigLayout layout = new MigLayout();
		
		drawPanel.setLayout(layout);
		
		this.setBackground(Color.white);
		this.setLayout(new BorderLayout());
		
		this.add(buttonBar, BorderLayout.NORTH);
		this.add(drawPanel, BorderLayout.CENTER);
	}
	
	/**
	 * Draw the rooms with specified coordinates.
	 * 
	 * @param x
	 * @param y
	 * @param width
	 * @param height
	 */
	public JPanel drawRoom(int x, int y, int width, int height, String roomName)
	{	
		JPanel rPanel = new JPanel();
		JLabel label = new JLabel(roomName);
		
		Border lineBorder = BorderFactory.createLineBorder(Color.BLACK);
		rPanel.setBorder(lineBorder);
		rPanel.add(label);
		
		// Add room to tracker.
		roomList.add(rPanel);
		
		String position = "pos " + x + " " + y;
		String widths = " ,w " + width + "!";
		String heights = " ,h " + height + "!";
		
		String vars = position + widths + heights;
		
		drawPanel.add(rPanel, vars);
		
		return rPanel;
	}
	
	public void highLightRooms(String value, Color color)
	{
		//if (value.equals(""))
	}
	
	/**
	 * Retrieve text field.
	 */
	public JTextField getTextField()
	{
		return nameField;
	}
	
	/**
	 * Register listener for camera button.
	 * @param e
	 */
	public void addCameraListener(ActionListener e)
	{
		cameraButton.addActionListener(e);
	}
	
	/**
	 * Register listener for search button.
	 */
	public void addSearchGuestListener(ActionListener e)
	{
		searchButton.addActionListener(e);
	}
	
	/**
	 * Register listener for combo box items.
	 */
	public void addHighLightChangeListener(ItemListener e)
	{
		highLightBox.addItemListener(e);
	}
	
	/**
	 * Retrieve list of room panels.
	 */
	public ArrayList<JPanel> getRoomPanelList()
	{
		return roomList;
	}
}
